package org.example.controller;

import lombok.RequiredArgsConstructor;
import org.example.bean.RedPacketActivity;
import org.example.enums.RedPacketStatus;
import org.example.service.RedPackage.GrabResult;
import org.example.service.RedPackage.RedPacketService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/red-packet")
@RequiredArgsConstructor
public class RedPacketController {
    private final RedPacketService redPacketService;

    // 检查红包状态
    @GetMapping("/status/{redPacketId}")
    public ResponseEntity<ResponseResult> checkStatus(@PathVariable("redPacketId") Integer redPacketId) {
        RedPacketStatus status = redPacketService.getRedPacketStatus(redPacketId);
        Map<String, Object> response = new HashMap<>();
        response.put("status", status.name());
        response.put("message", status.getMessage());
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(response).build());
    }

    // 抢红包
    @PostMapping("/grab/{activity_id}/{user_id}")
    public ResponseEntity<ResponseResult> grabRedPacket(
            @PathVariable("activity_id") Integer redPacketId,
            @PathVariable("user_id") Integer userId) {
        GrabResult result = redPacketService.grabRedPacket(userId, redPacketId);
        if (result.isSuccess()) {
            return ResponseEntity.ok(ResponseResult.builder().code(1).msg("抢红包成功").data(result).build());
        }
        return ResponseEntity.ok(ResponseResult.builder().code(0).msg("抢红包失败").data(result).build());
    }

    //创建红包活动
    @PostMapping("/create")
    public ResponseEntity<ResponseResult> createRedPacket(@RequestBody RedPacketActivity redPacketActivity) {
        redPacketService.createRedPacketActivity(redPacketActivity);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("创建成功").data(redPacketActivity).build());
    }

    //根据查询最新活动信息
    @GetMapping("/activity/last")
    public ResponseEntity<ResponseResult> getRedPacketActivity() {
        RedPacketActivity redPacketActivity = redPacketService.getRedPacketActivity();
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(redPacketActivity).build());
    }

    //查询所有的活动
    @GetMapping("/activity/all")
    public ResponseEntity<ResponseResult> getAllRedPacketActivity() {
        List<RedPacketActivity> redPacketActivityList = redPacketService.getAllRedPacketActivity();
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(redPacketActivityList).build());
    }
}